package com.anolesoft.yr.mbus.cont.dto;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.anolesoft.yr.mbus.cont.entity.ContContent;
import com.anolesoft.yr.mbus.contm.entity.ContmStyleItem;
import com.anolesoft.yr.mbus.contm.util.DocUtil;
import lombok.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
public class ContContentDTO implements Serializable {

    public ContContentDTO(JSONObject contmDtoJson, ContContentDTO parent, int orderCode, int lvl, List<ContmStyleItem> contmStyleItems){
        this.id  = contmDtoJson.getString("id");
        this.text = contmDtoJson.getString("text");
        this.html = contmDtoJson.getJSONObject("attributes").getString("html");
        this.contMainUid = contmDtoJson.getJSONObject("attributes").getString("contmMainUid");
        this.orderCode = Byte.parseByte(orderCode+"");
        this.parent = parent;
        this.pageType = contmDtoJson.getJSONObject("attributes").getString("contmMainUid")==null?contmDtoJson.getJSONObject("attributes").getString("contmMainUid"): getParent().getPageType();
        JSONArray children = contmDtoJson.getJSONArray("children");
        List<ContContentDTO> contContentDtos = new ArrayList<>();
        for (int i = 0; i < children.size(); i++) {
            contContentDtos.add(new ContContentDTO(children.getJSONObject(i),this,i,lvl+1,contmStyleItems));
        }
        this.lvl = lvl;
        if(pageType.equals("sy")){
            type = "sy_bt";
            contmStyleItems.forEach(a->{
                if(a.getItemType().equals("sy_bt")){
                    this.styleItemUid = a.getObjuid();
                }
            });

        }else if(pageType.equals("fy")){
            type = "fy_bt";
            contmStyleItems.forEach(a->{
                if(a.getItemType().equals("fy_bt")){
                    this.styleItemUid = a.getObjuid();
                }
            });
        }else if (pageType.equals("nr")){
            type = "nr_djbt";
            contmStyleItems.forEach(a->{
                if(a.getItemType().equals("nr_djbt")&&a.getLvl() == lvl){
                    this.styleItemUid = a.getObjuid();
                }
            });
        }else if(pageType.equals("qz")){
            this.type = "qz_bt";
            contmStyleItems.forEach(a->{
                if(a.getItemType().equals("qz_bt")){
                    this.styleItemUid = a.getObjuid();
                }
            });

        }else if(pageType.equals("fj")){
            if(lvl == 1){
                type = "fj_bt";
                contmStyleItems.forEach(a->{
                    if(a.getItemType().equals("fj_bt")){
                        this.styleItemUid = a.getObjuid();
                    }
                });

            }else{
                type = "fj_djbt";
                contmStyleItems.forEach(a->{
                    if(a.getItemType().equals("qz_bt")&&a.getLvl() == lvl-1){
                        this.styleItemUid = a.getObjuid();
                    }
                });

            }
        }
    }

     public ContContentDTO(ContContent content){
     //    ('sy_bt','fy_bt','fy_djbt','nr_djbt','fj_bt','fj_djbt';

         List<String> titleTypes = DocUtil.titleTypes();

         this.id = content.getObjuid();
         this.contMainUid = content.getContMainUid();
         this.pageType = content.getPageType();
         this.type =content.getItemType();
         this.order= content.getOrderCode();
         this.orderCode = content.getOrderCode();
         this.parentUid = content.getParentUid();
         content.getContContents().stream().
                 forEach(b->{

                     if(titleTypes.contains(b.getItemType())){
                        this.children.add(new ContContentDTO(b));
                        }else{
                         this.contContentsContent.add(new ContContentDTO(b));

                     }
                 });
         this.contContents.addAll(this.contContentsContent);
         this.contContents.addAll(this.children);
         if (this.contContentsContent.size()>0){
             html = this.contContentsContent.stream().map(ContContentDTO::getText).reduce((a, b)->a+b).get();
         }
     }
    private String id;

    private String contMainUid;

    private String pageType;

    private String type;

    private String html;

    private Byte orderCode;

    private String parentUid;

    private String text;

    private Integer lvl;

    private List<ContContentDTO> contContents;

    public List<ContContentDTO> children;

    public List<ContContentDTO> contContentsContent;

    private String contmStyleUid;

    private double order;

    private String styleItemUid;

    private ContContentDTO parent;

    public JSONObject toJson(){
        JSONObject attr = new JSONObject();
        JSONArray childrenJsonArray = new JSONArray();
        attr.put("content",html);
        attr.put("type",type);
        attr.put("contMmainUid",contMainUid);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id",id);
        jsonObject.put("text",text);
        jsonObject.put("attributes",attr);
        for (int i = 0; i < children.size(); i++) {
            ContContentDTO contDto = children.get(i);
            childrenJsonArray.add(contDto.toJson());
        }
        return jsonObject;
    }

}
